// 静态属性，样式属性，剥离数据 多种屏幕尺寸都事先部署好

const staticOption = {
  title: {
    text: '',
    left: 'center',
    top: '5%',
    textStyle: {
      color: '#d9e7ff',
      fontSize: 24,
      fontWeight: 'normal'
    }
  },
  tooltip: {
    name: '应用接入统计',
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  color: ['#30a0d1', '#2ac2e2', '#9ee2bd', '#ffdd89', '#ffa286', '#fb6c84', '#ee7eb2', '#e5beeb', '#7574ee',
    '#0069df'
  ],
  series: [{
    name: '',
    type: 'pie',
    roseType: 'radius',
    radius: ['30%', '50%'],
    avoidLabelOverlap: false,
    startAngle: '0',
    label: {
      show: true,
      fontSize: 22
    },
    data: []
  }]
}

const staticOptionMid = {
  title: {
    text: '',
    left: 'center',
    top: '5%',
    textStyle: {
      color: '#d9e7ff',
      fontSize: 18,
      fontWeight: 'normal'
    }
  },
  tooltip: {
    name: '应用接入统计',
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  color: ['#30a0d1', '#2ac2e2', '#9ee2bd', '#ffdd89', '#ffa286', '#fb6c84', '#ee7eb2', '#e5beeb', '#7574ee',
    '#0069df'
  ],
  series: [{
    name: '',
    type: 'pie',
    roseType: 'radius',
    radius: ['30%', '50%'],
    avoidLabelOverlap: false,
    startAngle: '0',
    label: {
      show: true,
      fontSize: 16
    },
    data: []
  }]
}

// 备选颜色，有需求则进行循环
// const colors = ['#0066ff', '#f16f3c', '#00ab47', '#df0050', '#394cff', '#702af2', '#8b0000', '#828e00', '#3700c4', '#730054']
// let colorIndex = 0

// 获取设备宽度
const screen = window.screen.width

class ChartFactory {
  constructor (additionalOptions) {
    // 根据屏幕宽度获取不同配置的echarts配置
    this.option = staticOption
    if (screen <= 1600) {
      this.option = staticOptionMid
    }
    // 下方复制属于浅拷贝，指向同个内存，针对可能有更改的需求，要进行一次清理
    this.clear()
    // 装配数据 必须使用箭头函数
    this.option.series[0].data = additionalOptions.data
    this.option.series[0].name = additionalOptions.name
    this.option.title.text = additionalOptions.title
  }

  // 清理方法，要将部署的新数据全部置空
  clear () {
    this.option.series[0].data = []
  }

  getOption () {
    return this.option
  }
}

export { ChartFactory }
